<?php

include_once 'connect.php';
include_once 'KmlGenerator.php';

//Checks if data which is necesary for queries has been set
if (isset($_POST['data'])) {
    $res = json_decode(stripslashes($_POST['data']), true);

    //if request is from bolge combobox it querys properly and returns the result
    if ($res['istek'] == 'bolge') {
        //According the value of data a query will be executed
        if ($res['bilgi'] == '0') {
            $query = mysql_query("select * from hatlar order by kod");
        } else if ($res['bilgi'] == '6') {
            $query = mysql_query("select * from hatlar where kod='METRO' or kod='ANKARAY'");
        } else if ($res['bilgi'] == '5') {
            $query = mysql_query("select * from hatlar where kod like '5%' or kod='9%' order by kod");
        } else if ($res['bilgi'] == '7') {
            $query = mysql_query("select * from hatlar where kod like 'DLMS%'");
        } else {
            $query = mysql_query("select * from hatlar where kod like '" . $res['bilgi'] . "%' order by kod");
        }

        $result = array();
        //populating the result. This result going to fill the 'hat' combobox
        while ($islem = mysql_fetch_array($query, MYSQL_ASSOC)) {
            array_push($result, $islem);
        }
        header("Content-type: text/plain");

        echo json_encode($result);
    } else if ($res['istek'] == 'hat') {
        //if the request is from hat
        
        //this checks as if the route is going to be full or partial.
        //for full route
        if(!is_array($res['bilgi'])) {
            $kod = $res['bilgi'];
            $query = mysql_query("select * from duraklar where kod='" . $kod . "'");
        } else {
            //for partial route
            $kod = $res['bilgi']['kod'];
            $query = mysql_query("select * from duraklar where kod='" . $kod . "' and sira>='".$res['bilgi']['fSira']."' and sira<='".$res['bilgi']['tSira']."'");
        }
        

        $result = array();
        $result['duraklar'] = array();
        //populating the duraklar array. this is the set that is going to be displayed
        while ($islem = mysql_fetch_array($query, MYSQL_ASSOC)) {
            $islem['renk'] = 'orange';
            array_push($result['duraklar'], $islem);
        }
        //gets the route id with route code
        $query = mysql_query("select aid from hatlar where kod='" . $kod . "'");

        $islem = mysql_fetch_array($query);
        $result['aid'] = $islem['aid'];
        
        //for full path displaying
        if(!is_array($res['bilgi'])) {
            $qu = "SELECT lat,lng FROM noktalar WHERE aid=" . $result['aid'] . " ORDER BY sira;";
        } else {
            //for partial path displaying
            
            //this query selects the nearest path point to the stop that user will get on
            $query = mysql_query("SELECT sira FROM noktalar WHERE aid=" . $result['aid'] . " ORDER BY ( 6371 * acos( cos( radians(".$res['bilgi']['fLng'].") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(".$res['bilgi']['fLat'].") ) + sin( radians(".$res['bilgi']['fLng'].") ) * sin( radians( lat ) ) ) ) LIMIT 1;");
            $islem = mysql_fetch_array($query);
            $fSira = $islem['sira'];
            //this query selects the nearest path point to the stop that user will get off
            $query2 = mysql_query("SELECT sira FROM noktalar WHERE aid=" . $result['aid'] . " ORDER BY ( 6371 * acos( cos( radians(".$res['bilgi']['tLng'].") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(".$res['bilgi']['tLat'].") ) + sin( radians(".$res['bilgi']['tLng'].") ) * sin( radians( lat ) ) ) ) LIMIT 1;");
            $islem = mysql_fetch_array($query2);
            $tSira = $islem['sira'];
            //this control necessary for bugs
            if($tSira<$fSira) {
                $tmp = $tSira;
                $tSira = $fSira;
                $fSira = $tmp;
            }
            //this query selects the point coordinates that is between two coordinates
            $qu = "SELECT lat,lng FROM noktalar WHERE aid=" . $result['aid'] . " and sira between '".$fSira."' and '".$tSira."' ORDER BY sira;";
            
        }
       
        //KML file is going to be generated with this query and other parameters
        if(KmlGenerator::createPath($qu,"file.kml", "7f0000ff")!="done") {
            echo "Hata : KML dosyası oluşturulamadı!";
        }
        
        header("Content-type: text/plain");
        echo json_encode($result);
    } else if ($res['istek'] == 'ikiHatGoster') {
        //if the request is from set points and needs two routes
        
        $akod = $res['bilgi']['akod'];
        //this query gets the stop informations that is between two point for first route
        $query = mysql_query("select * from duraklar where kod='" . $akod . "' and sira>='".$res['bilgi']['abin']."' and sira<='".$res['bilgi']['ain']."'");

        $result = array();
        $result['duraklar'] = array();

        //populating the duraklar array with orange markers for first route
        while ($islem = mysql_fetch_array($query, MYSQL_ASSOC)) {
            $islem['renk'] = 'orange';
            array_push($result['duraklar'], $islem);
        }
        
        $bkod = $res['bilgi']['bkod'];
        //this query gets the stop informations that is between two point for second route
        $query = mysql_query("select * from duraklar where kod='" . $bkod . "' and sira>='".$res['bilgi']['bbin']."' and sira<='".$res['bilgi']['bin']."'");

        //populating the duraklar array with blue markers for second route
        while ($islem = mysql_fetch_array($query, MYSQL_ASSOC)) {
            $islem['renk'] = 'blue';
            array_push($result['duraklar'], $islem);
        }

        //select two route id's
        $query = mysql_query("select aid from hatlar where kod='" . $akod . "'");

        $islem = mysql_fetch_array($query);
        $result['aaid'] = $islem['aid'];
        
        $query = mysql_query("select aid from hatlar where kod='" . $bkod . "'");

        $islem = mysql_fetch_array($query);
        $result['baid'] = $islem['aid'];
        
        
        //this query selects the nearest path point to the stop that user will get on for first route
        $query = mysql_query("SELECT sira FROM noktalar WHERE aid=" . $result['aaid'] . " ORDER BY ( 6371 * acos( cos( radians(".$res['bilgi']['a1lng'].") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(".$res['bilgi']['a1lat'].") ) + sin( radians(".$res['bilgi']['a1lng'].") ) * sin( radians( lat ) ) ) ) LIMIT 1;");
        $islem = mysql_fetch_array($query);
        $a1Sira = $islem['sira'];
        //this query selects the nearest path point to the stop that user will get off for first route
        $query = mysql_query("SELECT sira FROM noktalar WHERE aid=" . $result['aaid'] . " ORDER BY ( 6371 * acos( cos( radians(".$res['bilgi']['a2lng'].") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(".$res['bilgi']['a2lat'].") ) + sin( radians(".$res['bilgi']['a2lng'].") ) * sin( radians( lat ) ) ) ) LIMIT 1;");
        $islem = mysql_fetch_array($query);
        $a2Sira = $islem['sira'];
        if($a2Sira<$a1Sira) {
                $tmp1 = $a2Sira;
                $a2Sira = $a1Sira;
                $a1Sira = $tmp1;
            }
        //this query selects the nearest path point to the stop that user will get on for second route
        $query = mysql_query("SELECT sira FROM noktalar WHERE aid=" . $result['baid'] . " ORDER BY ( 6371 * acos( cos( radians(".$res['bilgi']['b1lng'].") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(".$res['bilgi']['b1lat'].") ) + sin( radians(".$res['bilgi']['b1lng'].") ) * sin( radians( lat ) ) ) ) LIMIT 1;");
        $islem = mysql_fetch_array($query);
        $b1Sira = $islem['sira'];
        //this query selects the nearest path point to the stop that user will get off for second route
        $query = mysql_query("SELECT sira FROM noktalar WHERE aid=" . $result['baid'] . " ORDER BY ( 6371 * acos( cos( radians(".$res['bilgi']['b2lng'].") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(".$res['bilgi']['b2lat'].") ) + sin( radians(".$res['bilgi']['b2lng'].") ) * sin( radians( lat ) ) ) ) LIMIT 1;");
        $islem = mysql_fetch_array($query);
        $b2Sira = $islem['sira'];
        if($b2Sira<$b1Sira) {
                $tmp2 = $b2Sira;
                $b2Sira = $b1Sira;
                $b1Sira = $tmp2;
            }
        //select path points between these points 
        $qu = "SELECT lat,lng FROM noktalar WHERE aid=" . $result['aaid'] . " and sira between '".$a1Sira."' and '".$a2Sira."' ORDER BY sira;";
        
        $qu2 = "SELECT lat,lng FROM noktalar WHERE aid=" . $result['baid'] . " and sira between '".$b1Sira."' and '".$b2Sira."' ORDER BY sira;";
        
        //Generate two seperate kml files for each path
        
        //for the first path
        if(KmlGenerator::createPath($qu,"file.kml","7f0000ff")!="done") {
            echo "Hata : KML dosyası oluşturulamadı!";
        }
        //for the second path
        if(KmlGenerator::createPath($qu2,"file2.kml","7fff0000")!="done") {
            echo "Hata : KML dosyası oluşturulamadı!";
        }
        
        header("Content-type: text/plain");
        echo json_encode($result);
    }
}
?>